package com.jlkj.web.shopnew.service.impl;

import com.google.common.collect.Maps;
import com.jlkj.web.shopnew.constant.Constant;
import com.jlkj.web.shopnew.constant.ConstantDateType;
import com.jlkj.web.shopnew.dto.DateDto;
import com.jlkj.web.shopnew.enums.EnumTimeType;
import com.jlkj.web.shopnew.pojo.User;
import com.jlkj.web.shopnew.pojo.mongo.CustomerBelongMongo;
import com.jlkj.web.shopnew.pojo.mongo.FollowMongo;
import com.jlkj.web.shopnew.pojo.mongo.LogMongo;
import com.jlkj.web.shopnew.request.GetAllUserByTypeRequest;
import com.jlkj.web.shopnew.request.GetUserInfoRequest;
import com.jlkj.web.shopnew.request.ModifyCustomerLabel;
import com.jlkj.web.shopnew.request.ModifyCustomerRequest;
import com.jlkj.web.shopnew.service.IStaff;
import com.jlkj.web.shopnew.service.IUser;
import com.jlkj.web.shopnew.service.mongo.ICustomerBelongMongo;
import com.jlkj.web.shopnew.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class CustomerBelongMongoImpl implements ICustomerBelongMongo {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IUser userService;

    @Autowired
    private IStaff staffService;


    @Override
    public void saveCustomerBelong(CustomerBelongMongo bean) {
        //修改名片  //修改客户类型
        Criteria criteria = new Criteria().andOperator(
                Criteria.where("uid").is(bean.getUid()),
                Criteria.where("belongUid").is(bean.getBelongUid())
        );

        Query query = Query.query(criteria);
        CustomerBelongMongo customerBelongMongo = this.mongoTemplate.findOne(query,
                CustomerBelongMongo.class);
        Date date = DateUtil.getMongoDate();
        if (customerBelongMongo != null) {
            Update update = new Update();

            if (bean.getUserType() == 0) {
                update.set("potentialTime", date);
                update.set("potentialType", 1);
                update.set("userType", 0);

            } else if (bean.getUserType() == 1) {
                update.set("intentionalTime", date);
                update.set("intentionalType", 1);
                update.set("userType", 1);

            } else if (bean.getUserType() == 2) {
                update.set("customerTime", date);
                update.set("userType", 2);
            }

            if (bean.getLabel() != null) {
                update.set("label", bean.getLabel());
                update.set("updateTime", date);
            }

            this.mongoTemplate.updateFirst(query, update, CustomerBelongMongo.class);

        } else {

            try {
                if (bean.getUserType() == 0) {
                    bean.setPotentialTime(date);
                    bean.setPotentialType(1);
                    bean.setUserType(0);
                } else if (bean.getUserType() == 1) {
                    bean.setIntentionalTime(date);
                    bean.setIntentionalType(1);
                    bean.setUserType(1);
                } else if (bean.getUserType() == 2) {
                    bean.setCustomerTime(date);
                    bean.setUserType(2);
                }
                User condi = new User();
                condi.setId(bean.getUid());
                User user = userService.getByCondition(condi);
                bean.setUserName(user == null ? "" : user.getUsername());

                User cond = new User();
                cond.setId(bean.getBelongUid());
                user = userService.getByCondition(cond);

                bean.setBelongUserName(user == null ? "" : user.getUsername());
                bean.setPhone(user == null ? "" : user.getPhone());
                mongoTemplate.save(bean, Constant.CUSTOMERBELONGMONGO);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void modifyCustomer(ModifyCustomerRequest request) {
        Criteria criteria = new Criteria().andOperator(
                Criteria.where("uid").is(request.getUid()),
                Criteria.where("belongUid").is(request.getBelongUid())
        );
        Date date = DateUtil.getMongoDate();
        Query query = Query.query(criteria);
        CustomerBelongMongo bean = this.mongoTemplate.findOne(query, CustomerBelongMongo.class);
        if (bean != null) {
            Update update = new Update();
            if (request.getUserType() == 0) {
                update.set("potentialTime", date);
                update.set("potentialType", 1);
                update.set("userType", 0);

            } else if (request.getUserType() == 1) {
                update.set("intentionalTime", date);
                update.set("intentionalType", 1);
                update.set("userType", 1);

            } else if (request.getUserType() == 2) {
                update.set("customerTime", date);
                update.set("userType", 2);
            }
            this.mongoTemplate.updateFirst(query, update, CustomerBelongMongo.class);

        }

        //修改follow
        //获取用户类型
       /* GetUserInfoRequest getUserInfoRequest = new GetUserInfoRequest();
        getUserInfoRequest.setOperationId(request.getBelongUid());
        getUserInfoRequest.setUid(request.getUid());
        int userType = this.getUserType(getUserInfoRequest);
        Criteria follow = new Criteria().andOperator(
                Criteria.where("uid").is(request.getUid()),
                Criteria.where("belongUid").is(request.getBelongUid())
        );

        Query queryFollow = Query.query(criteria);
        FollowMongo followMongo = this.mongoTemplate.findOne(queryFollow, FollowMongo.class);
        if (bean != null) {
            Update update = Update.update("userType", request.getUserType());
            this.mongoTemplate.updateFirst(query, update, FollowMongo.class);

        }
*/

    }

    @Override
    public void modifyCustomerLabel(ModifyCustomerLabel request) {
        Criteria criteria = new Criteria().andOperator(
                Criteria.where("uid").is(request.getUid()),
                Criteria.where("belongUid").is(request.getBelongUid())
        );

        Query query = Query.query(criteria);
        CustomerBelongMongo bean = this.mongoTemplate.findOne(query, CustomerBelongMongo.class);
        if (bean != null) {
            Update update = Update.update("label", request.getLabel());
            this.mongoTemplate.updateFirst(query, update, CustomerBelongMongo.class);

        }
    }

    @Override
    public int getUserType(GetUserInfoRequest getUserInfoRequest) {
        Criteria criteria = new Criteria().andOperator(
                Criteria.where("uid").is(getUserInfoRequest.getUid()),
                Criteria.where("belongUid").is(getUserInfoRequest.getOperationId())
        );

        Query query = Query.query(criteria);
        CustomerBelongMongo bean = this.mongoTemplate.findOne(query, CustomerBelongMongo.class);
        if (bean != null) {
            if (bean.getUserType() == 2) {
                return 2;
            } else if (bean.getIntentionalType() == 1 && bean.getUserType() == 1) {
                return 1;
            } else if (bean.getPotentialType() == 1 && bean.getUserType() == 0) {
                return 0;
            }

        }

        return 0;
    }

    @Override
    public String getUserLabel(GetUserInfoRequest getUserInfoRequest) {
        Criteria criteria = new Criteria().andOperator(
                Criteria.where("uid").is(getUserInfoRequest.getUid()),
                Criteria.where("belongUid").is(getUserInfoRequest.getOperationId())
        );

        Query query = Query.query(criteria);
        CustomerBelongMongo bean = this.mongoTemplate.findOne(query, CustomerBelongMongo.class);
        return bean == null ? "" : bean.getLabel();
    }

    @Override
    public Map<String, Object> getAllUserByType(GetAllUserByTypeRequest getAllUserByTypeRequest) {
        DateDto dateDto = DateUtil.getDayByType(getAllUserByTypeRequest.getDateType(),
                getAllUserByTypeRequest.getStartDay(), getAllUserByTypeRequest.getEndDay());

        /*String timeType = EnumTimeType.POTENTIALTIME.getTimeType();

        if (getAllUserByTypeRequest.getUserType() == 0) {
            timeType = EnumTimeType.POTENTIALTIME.getTimeType();
        } else if (getAllUserByTypeRequest.getUserType() == 1) {
            timeType = EnumTimeType.INTENTIONALTIME.getTimeType();
        } else if (getAllUserByTypeRequest.getUserType() == 2) {
            timeType = EnumTimeType.CUSTOMERTIME.getTimeType();
        }


        Criteria criteria = new Criteria();
        if (ConstantDateType.ALL_DAY.equals(getAllUserByTypeRequest.getDateType())) {
            criteria.and(timeType).lte(dateDto.getEndDay());
        } else {
            criteria.and(timeType).lte(dateDto.getEndDay()).gte(dateDto.getStartDay());
        }

        if (getAllUserByTypeRequest.isBoss()) {
            List<Integer> list = staffService.getCompanyStaff(getAllUserByTypeRequest.getUid());
            criteria.and("belongUid").in(list);
        } else {
            criteria.and("belongUid").is(getAllUserByTypeRequest.getUid());
        }

        //获取总人数
        long count = mongoTemplate.count(Query.query(criteria), CustomerBelongMongo.class);
*/
        int page = getAllUserByTypeRequest.getPage() == 0 ? 1 : getAllUserByTypeRequest.getPage();
        int pageSize = getAllUserByTypeRequest.getPageSize() == 0 ? 10 : getAllUserByTypeRequest.getPageSize();

        Criteria criteria1 = new Criteria();
        if (ConstantDateType.ALL_DAY.equals(getAllUserByTypeRequest.getDateType())) {
            criteria1.and("updateTime").lte(dateDto.getEndDay());
        } else {
            criteria1.and("updateTime").lte(dateDto.getEndDay()).gte(dateDto.getStartDay());
        }

        if (getAllUserByTypeRequest.isBoss()) {
            List<Integer> list = staffService.getCompanyStaff(getAllUserByTypeRequest.getUid());
            criteria1.and("operationId").in(list);
        } else {
            criteria1.and("operationId").is(getAllUserByTypeRequest.getUid());
        }

        criteria1.and("userType").is(getAllUserByTypeRequest.getUserType());


        PageRequest pageRequest = PageRequest.of(page - 1, pageSize,
                Sort.by(Sort.Direction.DESC, "createTime"));
        Query query = new Query(criteria1);
        //获取总客户数
        long totalCount = mongoTemplate.count(query, LogMongo.class);
        query.with(pageRequest);
        ///获取足迹
        List<LogMongo> list = mongoTemplate.find(query, LogMongo.class);

        Map<String, Object> data = Maps.newHashMap();
        //data.put("count", count);
        data.put("list", list);
        data.put("totalCount", totalCount);

        return data;
    }


}
